# encoding:utf-8
import requests
import re
from bs4 import BeautifulSoup


def get_html(url):
    headers = {
        'User-Agent': 'Mozilla/5.0(Macintosh; Intel Mac OS X 10_11_4)\
        AppleWebKit/537.36(KHTML, like Gecko) Chrome/52 .0.2743. 116 Safari/537.36'

    }  # 模拟浏览器访问
    response = requests.get(url, headers=headers)  # 请求访问网站
    response.encoding='gbk'
    html = response.text  # 获取网页源码
    return html  # 返回网页源码

def pr(url):
    soup = BeautifulSoup(get_html(url), 'lxml')  # 初始化BeautifulSoup库,并设置解析器
    #print(get_html('https://www.jianshu.com/'))
    for p in soup.select('a.ti'):
        h=p.get('href')
        sp=BeautifulSoup(get_html(h), 'lxml')
        for t in sp.select('div[accuse="aContent"]'):
            print (t)
            t.encoding='gbk'
            print (t.text)
            with open("text.txt",'a',encoding='utf-8') as f:
                f.write(t.text.encode('iso-8859-1').decode('gbk').replace('展开全部',''))

def prm(sta):
    for i in range(sta,sta+10):
        with open("Q.txt","r") as f:
            lines=f.readlines()
            try:
                print (lines[i])
            except:
                continue
            pr('https://zhidao.baidu.com/search?ct=17&pn=0&tn=ikaslist&rn=10&fr=wwwt&word='+lines[i])

print(get_html("https://zhidao.baidu.com/search?lm=0&rn=10&pn=0&fr=search&ie=gbk&word=%D7%AA%D5%CB%CA%A7%B0%DC%B5%C4%D4%AD%D2%F2%A3%BF"))